package com.brillilab.starter.controller.aio.team;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.pa.team.TeamPa;
import com.brillilab.domain.po.team.Team;
import com.brillilab.domain.vo.team.TeamDetail;
import com.brillilab.domain.vo.team.TeamInfoVo;
import com.brillilab.domain.vo.team.TeamMemberVo;
import com.brillilab.domain.vo.team.TeamSelectorVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.team.ITeamService;
import com.brillilab.service.logic.team.TeamLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 研究团队
 * @author WuMenghao XuZuE
 */
@RestController
@RequestMapping("/api/team")
public class TeamController {
    @Resource
    private TeamLogic teamLogic;
    @Resource
    private UsersLogic usersLogic;
    @Resource
    private ITeamService teamService;

    /**
     * 创建团队
     * @return
     */
    @AspectLog
    @RequestMapping(value="/add",method=RequestMethod.POST)
    public ResponseVo createTeam(@RequestBody TeamPa teamPa,@RequestHeader(StarterConstant.TOKEN)String token){
        Assert.isTrue(teamPa !=null && StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        Assert.isTrue(userInfo.getLabId()!=null,"用户未加入实验室！");
        boolean rs = teamLogic.createNewTeam(teamPa,userInfo.getLabId());
        return rs ? ResponseVo.success() : ResponseVo.failure();
    }

    /**
     * 编辑团队
     * @return
     */
    @AspectLog
    @RequestMapping(value="/edit",method=RequestMethod.POST)
    public ResponseVo editTeam(@RequestBody TeamPa teamPa,@RequestHeader(StarterConstant.TOKEN)String token){
        Assert.isTrue(teamPa!=null && StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        Assert.isTrue(userInfo.getLabId()!=null,"用户未加入实验室！");
        boolean rs = teamLogic.editTeam(teamPa,userInfo.getLabId());
        return rs ? ResponseVo.success() : ResponseVo.failure();
    }

    /**
     * 新增/编辑页面显示数据（未分组成员和组内成员）
     * @return
     */
    @RequestMapping(value="/detail",method=RequestMethod.GET)
    public ResponseVo teamDetail(Long teamId,@RequestHeader(StarterConstant.TOKEN)String token){
        Assert.isTrue(StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        Assert.isTrue(userInfo.getLabId()!=null,"用户未加入实验室！");
        TeamDetail teamDetail = teamLogic.getTeamDetail(teamId,userInfo.getLabId());
        return ResponseVo.success(teamDetail);
    }

    /**
     * 团队列表（查询未分组的成员和已经分组的团队）
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseVo selectTeam(@RequestHeader(StarterConstant.TOKEN) String token) {
        Assert.isTrue(StringUtils.isNotBlank(token), "请求参数缺失！");
        UserInfoVo userInfo = usersLogic.getUserByToken(token);
        Assert.isTrue(userInfo.getLabId() != null, "用户未加入实验室！");
        List<TeamInfoVo> isTeamList = teamLogic.isTeam(userInfo.getLabId());
        List<TeamMemberVo> noTeamList = teamLogic.noTeam(userInfo.getLabId());
        Team piTeam = teamLogic.getPiTeam(userInfo.getLabId());
        HashMap<String, Object> map = new HashMap<>();
        map.put("isTeamList", isTeamList);
        map.put("noTeamList", noTeamList);
        if(piTeam!=null){
            map.put("piTeamId",piTeam.getId());
            map.put("piTeamName",piTeam.getName());
        }
        return ResponseVo.success(map);
    }

    /**
     * 删除团队
     * @param teamId
     * */
    @AspectLog
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ResponseVo deleteTeam(Long teamId, @RequestHeader(StarterConstant.TOKEN) String token) {
        Assert.isTrue(teamId != null && StringUtils.isNotBlank(token), "请求参数缺失！");
        UserInfoVo userInfo = usersLogic.getUserByToken(token);
        Assert.isTrue(userInfo.getLabId() != null, "用户未加入实验室！");
        boolean rs = teamLogic.deleteTeam(userInfo.getLabId(), teamId);
        return rs ? ResponseVo.success() : ResponseVo.failure();
    }

    /**
     * 研究小组选择框
     * @param token
     * @return
     */
    @RequestMapping(value="/selector",method=RequestMethod.GET)
    public ResponseVo teamSelector(@RequestHeader(StarterConstant.TOKEN) String token){
        Assert.isTrue(StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        List<TeamSelectorVo> selectors  = teamService.getTeamSelector(userInfo.getLabId());

        Team piTeam=teamLogic.getPiTeam(userInfo.getLabId());
        Assert.notNull(piTeam,"PI研究小组未被初始化!");
        TeamSelectorVo teamSelectorVo=new TeamSelectorVo();
        teamSelectorVo.setId(piTeam.getId());
        teamSelectorVo.setName(piTeam.getName());

        List<TeamSelectorVo> rs=new ArrayList<>();
        rs.add(teamSelectorVo);
        rs.addAll(selectors.stream().filter(e -> !e.getId().equals(piTeam.getId())).collect(Collectors.toList()));
        return ResponseVo.success(rs);
    }

    /**
     * PI小组编辑
     * @return
     */
    @AspectLog
    @RequestMapping(value="/piEdit",method=RequestMethod.PUT)
    public ResponseVo piTeamEdit(@RequestBody TeamPa teamPa,@RequestHeader(StarterConstant.TOKEN)String token){
        Assert.isTrue(teamPa!=null && teamPa.getId()!=null && StringUtils.isNotBlank(teamPa.getName()),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        teamLogic.editPiTeam(teamPa.getId(),teamPa.getName(),userInfo);
        return ResponseVo.success();
    }
}
